import Food from "./food.js";
import gamePanel from "./gamePanel.js";
import Snake from "./snake.js";
class GameControl {
    snake: Snake;
    food: Food;
    gamePanel: gamePanel;
    // 创建属性来控制蛇移动方向
    direction: string = 'ArrowRight';
    // 设置参数记录蛇的生存状态
    isLive: boolean = true;
    // 设置重新开始游戏的按钮
    restartBtn: HTMLElement;
    constructor() {
        this.snake = new Snake();
        this.food = new Food();
        this.gamePanel = new gamePanel();
        this.restartBtn = document.getElementById(".restartBtn") as HTMLElement;
        this.init();//游戏初始化
    }
    init() {
        this.isLive = true;
        // 绑定键盘事件
        document.addEventListener("keydown", this.keyDownHandler.bind(this));
        this.run();
    }
    keyDownHandler(event: KeyboardEvent) {
        this.direction = event.key;//这里我们获取的是按下的键的名称
        // console.log(`当前按下的键为：${this.direction}`);
        // this.run();//不可以在这里调用run()方法，否则你每一次按下按键的时候都会开启一个定时器，速度直接爆炸！！！
        // 我们也可以使用keycode来确认方向
    }
    // 控制蛇跑的方法
    run() {
        // 首先获取当前蛇头的坐标
        let snakeX = this.snake.getX();
        let snakeY = this.snake.getY();
        // console.log(`当前蛇头的X为：${snakeX}`);
        // console.log(`当前蛇头的Y为：${snakeY}`);
        if (this.direction === 'ArrowUp') {
            snakeY -= 10;
            // console.log(`当前蛇头的Y为：${snakeY}`);

        } else if (this.direction === 'ArrowDown') {
            snakeY += 10;
            // console.log(`当前蛇头的Y为：${snakeY}`);
        } else if (this.direction === 'ArrowLeft') {
            snakeX -= 10;
            // console.log(`当前蛇头的X为：${snakeX}`);
        } else if (this.direction === 'ArrowRight') {
            snakeX += 10;
            // console.log(`当前蛇头的X为：${snakeX}`);
        }
        // console.log(snakeX, snakeY);

        // 更新蛇的位置
        try {
            this.snake.setX(snakeX);
            this.snake.setY(snakeY);
        } catch (e) {
            this.isLive = false;
            alert('打到等级6我叫你爸爸！');
        }

        // 判断蛇头的位置是否与食物的位置一致
        if (snakeX === this.food.getX() && snakeY === this.food.getY()) {
            // 判断一致后，需要先检查食物与蛇的位置不可以重合！！！
            this.checkFoodPosition();
            // 分数加一
            this.gamePanel.addScore();
            // 然后蛇的长度加一
            this.snake.snakegrow();

        }
        // 开启定时器
        this.isLive && setTimeout(this.run.bind(this), 100 - (this.gamePanel.level) * 20);
    }
    restart() {
        this.isLive = false;
        // 点击restartBtn按钮后重新开始游戏
        this.snake.setX(250);
        this.snake.setY(300);
        this.gamePanel.level = 1;
        this.gamePanel.score = 0;
        // 将蛇的身体清空
        const parentElement = this.snake.element;
        const childElements = parentElement.querySelectorAll('div');
        for (let i = 1; i < childElements.length; i++) {
            parentElement.removeChild(childElements[i]);
        }

        this.init();
    }
    // 检查食物的位置
    checkFoodPosition() {
        for (let i = 0; i < this.snake.bodies.length; i++) {
            if ((this.snake.bodies[i] as HTMLElement).offsetLeft === this.food.getX() && (this.snake.bodies[i] as HTMLElement).offsetTop === this.food.getY()) {
                // 食物与蛇的位置不可以重合
                this.food.foodPositionChange();
                break;
            } else {
                this.food.foodPositionChange();
            }
        }
    }
}
export default GameControl;
// const game = new GameControl();